//
//  HorizontalMenuView.m
//  HorizontalMenu
//
//  Created by Lj on 2016/11/10.
//  Copyright © 2016年 Lj. All rights reserved.
//

#define BUTTONTAG 687638

#import "HorizontalMenuView.h"

@interface HorizontalMenuView()

//背景
@property (nonatomic,strong)UIScrollView *scrollView;
//文字宽度
@property (nonatomic, strong) NSMutableArray *menuWidthArray;
@property (nonatomic, assign) CGFloat frameWidth;
@property (nonatomic, assign) CGFloat SPACE;
@property (nonatomic, assign) CGFloat titleWidth;
@end

@implementation HorizontalMenuView


- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self defaultParameters];
    }
    return self;
}

- (instancetype)init {
    if (self = [super init]) {
        [self defaultParameters];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self defaultParameters];
    }
    return self;
}

- (void)defaultParameters {
    self.frameWidth = self.frame.size.width;
    self.intervalLineHidden = YES;
    self.selectedSize = 16;
    self.selectedColor = [UIColor redColor];
    self.uncheckSize = 14;
    self.uncheckColor = [UIColor blackColor];
    self.lineColor = [UIColor redColor];
    self.intervalLineColor = [UIColor blueColor];
    self.intervalLineHeight = self.frame.size.width - 16;
    self.menuWidthArray = [NSMutableArray array];
    
}

- (void)horizontalMenuArray {
    
}



- (void)setNameWinthArray:(NSArray *)menuArray {
    NSAssert(menuArray.count != 0, @"数组为空");
    NSArray *array = [self.delegate horizontalMenuArray:self];
    NSLog(@"%@",array);

//    NSLog(@"===%@===",array[0]);
    
    //计算文字宽度
    for (int i = 0;i < menuArray.count ; i ++) {
        CGFloat width = [self widthForText:menuArray[i] StrSize:self.selectedSize];
        _titleWidth += width;
        [self.menuWidthArray addObject:[NSString stringWithFormat:@"%f",width]];
    }
    
    if (_titleWidth > self.frame.size.width) {
        self.scrollView.contentSize = CGSizeMake(_titleWidth, 0);
    }else {
        self.scrollView.contentSize = CGSizeMake(self.frame.size.width, 0);
        _SPACE += (self.frame.size.width - _titleWidth)/menuArray.count;
    }
    
    CGFloat fontWidth = 0.0;
    for (int i = 0; i < [menuArray count]; i++) {
        NSString *menuWidth;
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        if (i == 0) {
            menuWidth = @"0";
        }else {
            menuWidth = _menuWidthArray[i - 1];
            _titleWidth > self.frame.size.width?(fontWidth = fontWidth + [menuWidth floatValue]):(fontWidth = fontWidth + [menuWidth floatValue] + _SPACE);
        }
        button.frame = CGRectMake(fontWidth, 0, _titleWidth > self.frame.size.width?([_menuWidthArray[i] floatValue]):([_menuWidthArray[i] floatValue] + _SPACE), self.scrollView.frame.size.height);
        button.tag = i + BUTTONTAG;
        if (button.tag == BUTTONTAG) {
            button.enabled = NO;
        }
        
        //设置按钮的字体大小、颜色、状态(点击前)
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:[menuArray objectAtIndex:i]];
        [str addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:_uncheckSize],NSForegroundColorAttributeName:_uncheckColor} range:NSMakeRange(0,[str length])];
        [button setAttributedTitle:str forState:UIControlStateNormal];
        
        //(点击后)
        NSMutableAttributedString *selString = [[NSMutableAttributedString alloc]initWithString:[menuArray objectAtIndex:i]];
        [selString addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:_selectedSize],NSForegroundColorAttributeName:_selectedColor} range:NSMakeRange(0, [str length])];
        [button setAttributedTitle:selString forState:UIControlStateDisabled];
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:button];
    }
    
    if (!self.lineHidden) {
        [self markLineView];
    }
    //分割线
    if (!self.intervalLineHidden) {
        [self intervalLineView];
    }
}

//- (void)setIsLine:(BOOL)isLine {
//    if (isLine) {
//        NSAssert(!_menuWidthArray, @"数组为空");
//        [self markLineView];
//    }
//}


//标示当选被选中下划线
- (void)markLineView {
    CGFloat menuWidth = [_menuWidthArray[0] floatValue];
    UIView *markLineView = [[UIView alloc]initWithFrame:CGRectMake(0, self.frame.size.height - 2, menuWidth + _SPACE, 2)];
    markLineView.tag = 999;
    markLineView.backgroundColor = self.lineColor;
    [self.scrollView addSubview:markLineView];
}

//间隔线
- (void)intervalLineView {
    for (int i = 0; i < _menuWidthArray.count; i ++) {
        CGFloat segmentationLine;
        if ( i> 0 && self.frame.size.height > 16) {
            UIView *lineView = [[UIView alloc]init];
            _titleWidth > self.frameWidth?(segmentationLine += [self.menuWidthArray[i - 1]floatValue]):(segmentationLine += ([self.menuWidthArray[i - 1]floatValue] + _SPACE));
            lineView.frame = CGRectMake(segmentationLine, (self.frame.size.height - self.intervalLineHeight)/2 , 1, self.intervalLineHeight);
            lineView.backgroundColor = self.intervalLineColor;
            [self.scrollView addSubview:lineView];
        }
    }
}

- (void)setSelectButton:(NSInteger)item {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.tag = item + BUTTONTAG;
    [self buttonClick:button];
}

//按钮点击
-(void)buttonClick:(UIButton *)sender{
    for (UIView *subView in self.scrollView.subviews) {
        if ([subView isKindOfClass:[UIButton class]]) {
            UIButton *subButton = (UIButton *)subView;
            if (subButton.tag == sender.tag) {
                [subButton setEnabled:NO];
            }else{
                [subButton setEnabled:YES];
            }
        }
    }
    [self.delegate menuView:self didSelectButton:sender.tag - BUTTONTAG];

    [self mobileMarkLineView:sender];
}

//底部划线移动
- (void)mobileMarkLineView:(UIButton *)sender {
    CGFloat distance = 0.0 , displacement = 0.0;
    for (int i = 0; i < sender.tag - BUTTONTAG; i++) {
        _titleWidth > self.frameWidth?(distance += [_menuWidthArray[i] floatValue]):(distance += ([_menuWidthArray[i] floatValue] + _SPACE));
    }
    CGFloat merkWidth = [_menuWidthArray[sender.tag - BUTTONTAG] floatValue] + _SPACE;
    
    UIView *markView = [self viewWithTag:999];
    [UIView animateWithDuration:0.2f animations:^{
        CGRect markFrame = markView.frame;
        markFrame.size.width = merkWidth;
        markFrame.origin.x = distance;
        markView.frame = markFrame;
    }];
    
    if ((distance + merkWidth/2) >= self.frameWidth/2) {
        if (self.titleWidth - distance < self.frameWidth/2) {
            displacement = self.titleWidth - self.frameWidth;
//            displacement = 0;
        }else {
            displacement = (distance + merkWidth/2) - self.frameWidth/2;
        }
    }
    [UIView animateWithDuration:0.5f animations:^{
        self.scrollView.contentOffset = CGPointMake(displacement, 0);
    }];
}

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        _scrollView.backgroundColor = self.backgroundColor;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        [self addSubview:_scrollView];
    }
    return _scrollView;
}

//计算Label的宽度
- (CGFloat)widthForText:(NSString *)string StrSize:(NSInteger)strSize{
    CGSize size =[string sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:strSize]}];
    return size.width + 20;
}

@end
